$(function () {

    var member_datagrid = $("#member_datagrid");
    var member_dialog = $("#member_dialog");
    var memberpass_dialog = $("#memberpass_dialog");

    //员工列表初始化-begin
    $("#member_datagrid").datagrid({
        url: "/member/list",
        fit: true,
        fitColumns: true,
        pagination: true,
        striped: true,
        singleSelect: true,
        pageList: [9],
        pageSize: 9,
        columns: [[
            {field: "", checkbox: true},  //显示多选框样式
            {field: "id", title: "编号", hidden: true},
            {field: "cardno", title: "会员卡号", width: 100},
            {field: "name", title: "会员姓名", width: 100},
            {field: "tel", title: "电话", width: 100},
            {field: "level", title: "等级", width: 100,formatter: function (value, row, index) {
                return value.levelName;
            }},
            {field: "availablepoint", title: "可用积分", width: 100},
            {field: "balance", title: "卡内余额", width: 100},
            {field: "totalpoint", title: "累计积分", width: 100},
            {field: "startdate", title: "开卡时间", width: 100},
            {
                field: "state", title: "状态", width: 100, formatter: function (value, row, index) {
                if (row.state) {
                    return "<font style='color: green'>正常</font>";
                } else {
                    return "<font style='color: red'>已挂失</font>";
                }
            }
            }
        ]],
        //绑定行选中事件,变更对应离复职按钮状态
        onClickRow: function (index, row) {
            if (row.state) {
                $("#btn_changeState").linkbutton({
                    text: "挂失"
                });
            } else {
                $("#btn_changeState").linkbutton({
                    text: "恢复"
                });
            }
        }

    })
    //员工列表初始化-end


    //初始化新增和编辑的dialog
    member_dialog.dialog({
        width: 600,
        height: 450,
        buttons: "#bb",
        draggable: true,
        resizable: true,
        closed: true
    })

    //修改密码对话框的初始化
    memberpass_dialog.dialog({
        width: 400,
        height: 300,
        buttons: "#pp",
        draggable: true,
        resizable: true,
        closed: true
    })

    //换卡对话框的初始化
    $("#membercardno_dialog").dialog({
        width: 400,
        height: 300,
        title:'会员补卡',
        buttons: "#cc",
        draggable: true,
        resizable: true,
        closed: true
    })



    //密码的确认的校验规则
    $.extend($.fn.validatebox.defaults.rules, {
        equals: {
            validator: function (value, param) {
                return value == $(param[0]).val();
            },
            message: '两次输入密码不一致.'
        }
    });

    //同一管理响应函数
    var cmdObj = {
        //顶部工具栏的刷新
        reload: function () {
            member_datagrid.datagrid("load");
        },
        //工具栏的编辑
        edit: function () {
            //是否选中row
            var row = member_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择要编辑的数据");
                return;
            }
            if (!row.state) {
                $.messager.alert("温馨提示", "卡片已挂失,请先恢复!");
                return;
            }
            //清空上次操作的数据
            $("#member_form").form("clear");
            //form的同名加载原则

            //回显选中的行
            //设置标题
            member_dialog.dialog("setTitle", "会员编辑");
            //禁止修改密码,开启只读
            $("#password").passwordbox("readonly", true);
            //打开窗口
            member_dialog.dialog("open");
            $("#member_form").form("load", row);
        },
        //工具栏:添加
        add: function () {
            //清空上次操作的数据
            $("#member_form").form("clear");
            //设置标题
            member_dialog.dialog("setTitle", "会员新增");
            $("#password").passwordbox("readonly", false);
            //打开窗口
            member_dialog.dialog("open");
        },
        //工具栏:挂失
        changeState: function () {
            //是否选中row
            var row = member_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择要操作的数据");
                return;
            }
            //重要操作:提示用户
            $.messager.confirm('确认', '您确认想要执行这个操作？', function (r) {
                if (r) {
                    //确定要进行操作
                    //修改选中行的状态(与原来相反)
                    //发请求修改原先状态值
                    $.post("/member/changeState", {id: row.id}, function (data) {
                       // data = $.parseJSON(data);
                        if (data.success) {
                            //修改成功就刷新列表
                            member_datagrid.datagrid("load");
                        } else {
                            $.messager.alert("温馨提示", data.errorMsg);
                        }
                    })
                }
            });
        },
        //顶部工具栏:高级查询
        query: function () {
            //获取查询参数
            var kw = $("#keyword").textbox("getValue");
            //表格加载时顺带一起传递查询参数
            member_datagrid.datagrid("load", {
                keyword: kw
            });
        },
        //底部按钮:保存
        save: function () {
            //默认是保存请求
            var url = "member/save";
            var empId = $("#memberId").val();
            if (empId) {
                //回显的empId有值则是update
                url = "member/update";
            }
            $('#member_form').form('submit', {
                url: url,
                //表单提交成功后
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (!data.success) {
                        $.messager.alert('温馨提示', data.errorMsg);
                    } else {
                        //刷新列表
                        member_datagrid.datagrid("load");
                        //关闭当前对话框
                        member_dialog.dialog("close");
                    }
                }
            });

        },

        //顶部工具栏修改密码按钮
        changePassword: function () {
            //是否选中row
            var row = member_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择要修改密码的会员!");
                return;
            }
            if (!row.state) {
                $.messager.alert("温馨提示", "卡片已挂失,请先恢复!");
                return;
            }
            //清空上次操作的数据
            $("#memberpass_form").form("clear");
            //form的同名加载原则

            //设置标题
            memberpass_dialog.dialog("setTitle", "修改密码");
            //隐藏显示选中的行,只有id
            $("#memberpass_form").form("load", row);
            //打开窗口
            memberpass_dialog.dialog("open");
        },

        //修改密码底部按钮:确认修改
        comfirmpass: function () {
            //默认是保存请求
            var url = "member/changePassword";
            $('#memberpass_form').form('submit', {
                url: url,
                //表单提交成功后
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (!data.success) {
                        $.messager.alert('温馨提示', data.errorMsg);
                    } else {
                        //关闭当前对话框
                        memberpass_dialog.dialog("close");
                        $.messager.alert('温馨提示', "修改成功!");
                        //刷新列表
                        member_datagrid.datagrid("load");
                    }
                }
            });

        },

        //顶部-补卡按钮
        changeCardno: function () {
            //是否选中row
            var row = member_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "请选择要补卡的会员!");
                return;
            }
            //清空上次操作的数据
            $("#membercardno_form").form("clear");

            //回显
            $("#membId").val(row.id);
            $("#oldcardno").textbox("setValue", row.cardno);

            //打开窗口
            $("#membercardno_dialog").dialog("open");
        },
        //确认补卡按钮
        comfirmcardno:function (){
            $('#membercardno_form').form('submit', {
                url: '/member/changeCardno',
                //表单提交成功后
                success: function (data) {
                    var data = $.parseJSON(data);
                    if (!data.success) {
                        $.messager.alert('温馨提示', data.errorMsg);
                    } else {
                        //关闭当前对话框
                        $("#membercardno_dialog").dialog("close");
                        $.messager.alert('温馨提示', "换卡成功!");
                        //刷新列表
                        member_datagrid.datagrid("load");
                    }
                }
            });
        },





        //编辑添加底部按钮:取消
        cancel: function () {
            $(".moreinfo").css("display", "none");
            member_dialog.dialog("close");
        },
        //编辑添加更多按钮
        more: function () {
            $("tr:hidden").css("display", "");
        },
        //修改密码底部按钮
        cancelpass: function () {
            memberpass_dialog.dialog("close");
        }



    } //end

    //调用cmdObj
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data('cmd');
        cmdObj[cmd]();
    })

    //网页加载完毕后通过ajax载入四个会员统计表格信息
    $.post("/member/analysisInfo",function (data) {
       // data = $.parseJSON(data);
        $("#todayBirthNumber").text(data.todayBirthNumber+"位");
        $("#otherBirthNumber").html("本月还有<font color='red'>"+ data.otherBirthNumber +"</font>位会员生日");
        $("#todayMemberNumber").text(data.totalMemberNumber+"位");
        $("#totalBalance").html("储值卡总额:<font color='red'>¥"+data.totalBalance +"</font>");
        $("#totalSpend").text("¥"+data.totalSpend);
        $("#top1").html("1."+ data.top3Member[0].name + "<font color='red'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+ data.top3Member[0].totalSpend +"</font>元");
        $("#top2").html("2."+ data.top3Member[1].name + "<font color='red'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+ data.top3Member[1].totalSpend +"</font>元");
        $("#top3").html("3."+ data.top3Member[2].name + "<font color='red'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"+ data.top3Member[2].totalSpend +"</font>元");



    });

})